/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;

import cn.com.chinasoa.course.subscribe.model.ArrangedCourse;
import cn.com.chinasoa.course.subscribe.model.PrivilegeStrategy;
import cn.com.chinasoa.course.subscribe.model.SubscribeOrder;
import cn.com.chinasoa.course.subscribe.util.CommonFieldValidator;
import cn.com.chinasoa.course.subscribe.util.CommonFiledChecker;
import cn.com.chinasoa.course.subscribe.util.DataTypeUtil;
import cn.com.chinasoa.course.subscribe.util.EntityManagerHelper;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-1上午02:08:39
 */
public class SubscribeOrderCreateActionFilter extends BaseRequestFilter
		implements ActionFilter {

	private static String SUCCESS = "success";
	private static String FAILED = "error";
	
	public static final String ORDER_ID = SubscribeOrderCreateActionFilter.class.getName()+"_ORDER_ID";
	public static final String FORM_ID = SubscribeOrderCreateActionFilter.class.getName()+"_FORM_ID";
	
	public String process(ActionRequest actionRequest, ActionResponse actionResponse,
			ActionConfig actionConfig) throws PortletException, IOException {

		boolean success = false;

		CommonFieldValidator.clearNoticeText(FORM_ID, actionRequest.getPortletSession());
		CommonFiledChecker.clearFieldValue(FORM_ID, actionRequest.getPortletSession());

/*		String studentName = actionRequest.getParameter("studentName");
		String studentSex = actionRequest.getParameter("studentSex");
		String studentEmail = actionRequest.getParameter("studentEmail");
		String studentCompany = actionRequest.getParameter("studentCompany");
		String studentPhone = actionRequest.getParameter("studentPhone");
		String mobilePhone = actionRequest.getParameter("mobilePhone");
		String studentAddress = actionRequest.getParameter("studentAddress");
		
		String arrangedCourse = actionRequest.getParameter("arrangedCourse");*/

		String privilegeStrategy = actionRequest.getParameter("privilegeStrategy");
		
		String studentName = CommonFiledChecker.checkParamValue(FORM_ID, "studentName", actionRequest);
		String studentSex = CommonFiledChecker.checkParamValue(FORM_ID, "studentSex", actionRequest);
		String studentEmail = CommonFiledChecker.checkParamValue(FORM_ID, "studentEmail", actionRequest);
		String studentCompany = CommonFiledChecker.checkParamValue(FORM_ID, "studentCompany", actionRequest);
		String studentPhone = CommonFiledChecker.checkParamValue(FORM_ID, "studentPhone", actionRequest);
		String mobilePhone = CommonFiledChecker.checkParamValue(FORM_ID, "mobilePhone", actionRequest);
		String studentAddress = CommonFiledChecker.checkParamValue(FORM_ID, "studentAddress", actionRequest);
		
		String arrangedCourse = CommonFiledChecker.checkParamValue(FORM_ID, "arrangedCourse", actionRequest);
		
		if((studentName!=null)&&(studentSex!=null)&&(studentEmail!=null)&&(studentCompany!=null)&&
				(studentAddress!=null)&&(studentPhone!=null)&&(mobilePhone!=null)&&(arrangedCourse!=null)&&
				(!studentName.equals(""))&&(!studentSex.equals(""))&&(!studentEmail.equals(""))&&(!studentCompany.equals(""))&&
				(!studentAddress.equals(""))&&(!studentPhone.equals(""))&&(!mobilePhone.equals(""))&&(!arrangedCourse.equals(""))){

			EntityManager em = EntityManagerHelper.getEntityManager();
			em.getTransaction().begin();

			SubscribeOrder vSubscribeOrder = new SubscribeOrder();
			
			vSubscribeOrder.setStudentName(studentName);
			vSubscribeOrder.setStudentEmail(studentEmail);
			vSubscribeOrder.setStudentCompany(studentCompany);
			vSubscribeOrder.setStudentAddress(studentAddress);
			vSubscribeOrder.setStudentPhone(studentPhone);
			vSubscribeOrder.setStudentSex(DataTypeUtil.parseBoolean(studentSex));
			vSubscribeOrder.setMobilePhone(mobilePhone);
			
			ArrangedCourse tArrangedCourse = em.find(ArrangedCourse.class, DataTypeUtil.parseLong(arrangedCourse));
			vSubscribeOrder.setArrangedCourse(tArrangedCourse);

			if(privilegeStrategy!=null){
				PrivilegeStrategy tPrivilegeStrategy = em.find(PrivilegeStrategy.class, DataTypeUtil.parseLong(privilegeStrategy));
				vSubscribeOrder.setPrivilegeStrategy (tPrivilegeStrategy);
			}
			em.persist(vSubscribeOrder);
			em.getTransaction().commit();

			long orderId = vSubscribeOrder.getId();
			EntityManagerHelper.closeEntityManager();

			SubscribeOrderCreateActionFilter.setOrderId(orderId, actionRequest);

			CommonFieldValidator.clearNoticeText(FORM_ID, actionRequest.getPortletSession());
			success = true;
		}else{
			success = false;
		}

		if(success)return SUCCESS;
		else return FAILED;
	}
	
	public static long getOrderId(PortletRequest portletRequest){
		long orderId = -1;
		Object orderIdObj = portletRequest.getPortletSession().getAttribute(SubscribeOrderCreateActionFilter.ORDER_ID);
		if((orderIdObj!=null)&&(orderIdObj instanceof Long)){
			orderId = (Long)orderIdObj;
		}
		return orderId;
	}
	
	public static void setOrderId(long orderId,PortletRequest portletRequest){
		portletRequest.getPortletSession().setAttribute(SubscribeOrderCreateActionFilter.ORDER_ID,orderId);		
	}	

	public static void clearOrderId(PortletRequest portletRequest){
		portletRequest.getPortletSession().removeAttribute(SubscribeOrderCreateActionFilter.ORDER_ID);		
	}

}
